package com.xujianlun.cpms.controller.sys;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xujianlun.cpms.common.Response;
import com.xujianlun.cpms.dto.LocalNameDto;
import com.xujianlun.cpms.entity.sys.Locale;
import com.xujianlun.cpms.exception.ServiceException;
import com.xujianlun.cpms.mapper.sys.LocalNameMapper;
import com.xujianlun.cpms.service.sys.ILocaleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xujianlun.cpms.service.sys.ILocalNameService;
import com.xujianlun.cpms.entity.sys.LocalName;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 地点名 前端控制器
 * </p>
 *
 * @author xujianlun
 * @since 2022-11-30
 */
@RestController
@RequestMapping("/localName")
public class LocalNameController {

    @Resource
    private ILocalNameService localNameService;
    @Resource
    private ILocaleService localeService;
    @Resource
    private LocalNameMapper localNameMapper;
    // 新增或者更新
    @PostMapping
    public Response save(@RequestBody LocalName localName) {
        if (localName.getId()!=null){
            localNameService.updateById(localName);
            return Response.success();
        }
        QueryWrapper<LocalName> localNameQueryWrapper = new QueryWrapper<>();
        localNameQueryWrapper.eq("locale_name",localName.getLocaleName());
        localNameQueryWrapper.eq("locale_id",localName.getLocaleId());
        LocalName one = localNameService.getOne(localNameQueryWrapper);
        if (one != null){
            throw new ServiceException(-200,"已存在该区域");
        }
        localNameService.save(localName);
        return Response.success();
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {

        return Response.success(localNameService.removeById(id));
    }

    @PostMapping("/batch")
    public Response deleteBatch(@RequestBody List<Integer> ids) {

        return Response.success(localNameService.removeByIds(ids));
    }

    @GetMapping
    public Response findAll() {
        return Response.success(localNameService.list());
    }

    @GetMapping("/{id}")
    public Response findOne(@PathVariable Integer id) {
        return Response.success(localNameService.getById(id));
    }

    @GetMapping("/page")
    public Response findPage(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam String  localeName,
                             @RequestParam String  name) {
        return Response.success(localNameMapper.page(new Page<>(pageNum,pageSize),localeName,name));
    }
    /**
     * 查询localname
     *
     */
    @GetMapping("/byLocale/{name}")
    public Response findByLocalName(@PathVariable String name){
        QueryWrapper<Locale> localeQueryWrapper = new QueryWrapper<>();
        localeQueryWrapper.eq("name",name);
        Locale locale = localeService.getOne(localeQueryWrapper);
        //查询到local的ID
        Integer id = locale.getId();
        //然后根据locale的ID来查localName
        QueryWrapper<LocalName> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("locale_id",id);
        List<LocalName> localNameList = localNameService.list(queryWrapper);
        return Response.success(localNameList);
    }
}

